//
//  UIViewControllerExtension.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/11.
//

import Foundation
import UIKit

extension UIViewController{
    private struct AssociatedKey{
        static var parameter = "custom_parameter"
        static var placeholder = "placeholder"
        private init() {
            
        }
    }
    var parameter:Dictionary<String,Any>?{
        get{
            return objc_getAssociatedObject(self, &AssociatedKey.parameter) as? Dictionary<String,Any>
        }
        set{
            objc_setAssociatedObject(self, &AssociatedKey.parameter, newValue, .OBJC_ASSOCIATION_COPY)
        }
    }
    var navigationBarHeight:CGFloat{
        guard self.navigationController != nil else {
            return 44.0+UIApplication.shared.statusBarFrame.height
        }
        return (self.navigationController?.navigationBar.frame.height)!+UIApplication.shared.statusBarFrame.height
    }
}

extension UIViewController:CustomMethodSwizzling{
    var placeholderView:PlaceholderView?{
        set{
            objc_setAssociatedObject(self, &AssociatedKey.placeholder, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get{
            return objc_getAssociatedObject(self, &AssociatedKey.placeholder) as? PlaceholderView
        }
    }
    static func awake() {
        swizzling
    }
    private static let swizzling:()={
        let oldSelector = #selector(viewDidLayoutSubviews)
        let newSelector = #selector(customLayoutSubviews)
        swizzlingForClass(UIViewController.self, oldSelector: oldSelector, newSelector: newSelector)
    }()
    
    @objc func customLayoutSubviews() {
        customLayoutSubviews()
        self.checkNetWork()
    }
    private func checkNetWork(){
        self.placeholderView?.removeFromSuperview()
        if placeholderView != nil{
            view.addSubview(self.placeholderView!)
            self.placeholderView?.frame = view.bounds
        }
    }
}


